package schoolWork.work05.test6;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MyJFrame extends JFrame implements ActionListener {
    JFrame jFrame = new JFrame();

    JButton[] btns = new JButton[6];
    JButton btn = new JButton("讲台");

    public MyJFrame() {
        for (int i = 0; i < 6; i++) {
            btns[i] =  new JButton("座位"+i);
        }
        jFrame.setLayout(new BorderLayout());

        JPanel jPanel1 = new JPanel(new GridLayout(1,1));
        JPanel jPanel2 = new JPanel(new GridLayout(2,3));

        jPanel1.add(btn);
        for (int i = 0; i < 6; i++) {
            jPanel2.add(btns[i]);
        }

        btn.addActionListener(this);
        for (int i = 0; i < 6; i++) {
            btns[i].addActionListener(this);
        }
        jFrame.add(jPanel1,BorderLayout.NORTH);
        jFrame.add(jPanel2,BorderLayout.CENTER);

        jFrame.setSize(500,400);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setLocationRelativeTo(null);
        jFrame.setVisible(true);

    }


    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == btn)
            System.out.println("讲台不可以坐");
        else
            for (int i = 0; i < 6; i++) {
                if(e.getSource() == btns[i])
                System.out.println("你选择了座位"+i);
            }
    }
}
